package com.tamartech.greendb.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;

import com.tamartech.greendb.data.ElectoralRoll;
import com.tamartech.greendb.data.Ward;


public class ElectoralRollDAO extends DAO
{
   public ElectoralRoll save (ElectoralRoll a) 
         throws Exception
   {
      try
      {
         begin();
         getSession().save(a);
         commit();
         return a;
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not create ElectoralRoll", e);
      }
   }


//   public void update(ElectoralRoll a) throws Exception 
//   {
//      try 
//      {
//         begin();
//         getSession().update(a);
////         getSession().save(a);
//         commit();
//      } 
//      catch (HibernateException e) 
//      {
//         rollback();
//         throw new Exception("Could not save ElectoralRoll", e);
//      }
//   }

   public ElectoralRoll get (Long id) throws Exception 
   {
      try 
      {
         begin();
         ElectoralRoll a = (ElectoralRoll) getSession().load(ElectoralRoll.class, id);
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain ElectoralRoll " + id, e);
      }
   }
   
/* ?? Do I still need this? Will need modifying if so
   public ElectoralRoll getLatest (String constituencyName) throws Exception 
   {
      ElectoralRoll er = null;
      try 
      {
         begin();
         // perhaps a bit naughty to use SQL rather than HQL
         String sql = "SELECT distinct {er.*} FROM `address_constituency` ac,\n" +
                      "       electoral_roll er,\n" +
                      "       constituency c\n" +
                      "WHERE ac.electoral_roll = er.id\n" +
                      "and ac.constituency = c.id\n" +
                      "and c.name = '" + constituencyName + "'\n" +
                      "order by er.compile_date desc\n" +
                      "limit 1";

         SQLQuery q = getSession().createSQLQuery (sql);
         q.addEntity ("er", ElectoralRoll.class);
         List<ElectoralRoll> list = q.list();
         
         if (list.size() > 0)
             er = list.get(0);
         
         commit();
         return er;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain ElectoralRoll " + constituencyName, e);
      }
   }
*/
   
   public ElectoralRoll getByDate (Ward w, Date d) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from ElectoralRoll where ward = :ward and date = :date");
         q.setEntity ("ward", w);
         q.setDate ("date", d);

         ElectoralRoll found = (ElectoralRoll) q.uniqueResult();
         commit();
         return found;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain ElectoralRoll for ward: " + w + ". date: " + d, e);
      }
   }
   
   public ElectoralRoll getLatestByWard (Ward w) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from ElectoralRoll where ward = :ward order by date desc");
         q.setEntity ("ward", w);
         q.setMaxResults(1);

         ElectoralRoll found = (ElectoralRoll) q.uniqueResult();
         commit();
         return found;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain latest ElectoralRoll for ward: " + w, e);
      }
   }
   
   
/*
   public ElectoralRoll get (String name) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from ElectoralRoll where name = :name");
         q.setString ("name", name);
         ElectoralRoll a = (ElectoralRoll) q.uniqueResult();
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain ElectoralRoll " + name, e);
      }
   }
*/
   @SuppressWarnings("unchecked")
   public List<ElectoralRoll> list() throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from ElectoralRoll");
         List<ElectoralRoll> list = q.list();
         commit();
         return list;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not list the electoralRolls", e);
      }
   }

   
   public void delete(ElectoralRoll a)
      throws Exception
   {
      try
      {
         begin();
         getSession().delete(a);
         commit();
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not delete ElectoralRoll", e);
      }
   }
}
